<template>
  <div class="member-classify-container">
    <el-card class="header">
      <div>
        <el-button
          type="success"
          @click="classifyAdd"
          v-permission="['memberClassifyAdd']"
          >{{ $t('msg.classifyInfo.classifyAdd') }}</el-button
        >
        <el-button
          class="float-right"
          type="primary"
          @click="onImportExcelClick"
          v-permission="['treatDiscountSet']"
          >{{ $t('msg.classifyInfo.treatDiscountSet') }}</el-button
        >
      </div>
    </el-card>
    <el-card>
      <el-table
        :data="allMemberClassifies"
        border
        style="width: 100%"
        row-key="id"
      >
        <el-table-column
          align="center"
          label="#"
          type="index"
        ></el-table-column>
        <el-table-column
          align="center"
          :label="$t('msg.member.classify')"
          prop="classifyName"
        ></el-table-column>
        <el-table-column
          align="center"
          :label="$t('msg.classifyInfo.registerDisc')"
        >
          <template #default="{ row }">
            {{ $filters.discountFilterShow(row.registerDisc) }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="$t('msg.classifyInfo.diagnoseDisc')"
        >
          <template #default="{ row }">
            {{ $filters.discountFilterShow(row.diagnoseDisc) }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="$t('msg.classifyInfo.recipeDisc')"
        >
          <template #default="{ row }">
            {{ $filters.discountFilterShow(row.recipeDisc) }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="$t('msg.classifyInfo.treatDisc')"
        >
          <template #default="{ row }">
            {{ $filters.discountFilterShow(row.treatDisc) }}
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('msg.classifyInfo.goodsDisc')"
          align="center"
        >
          <template #default="{ row }">
            {{ $filters.discountFilterShow(row.goodsDisc) }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="$t('msg.classifyInfo.otherDisc')"
        >
          <template #default="{ row }">
            {{ $filters.discountFilterShow(row.otherDisc) }}
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('msg.btn.action')"
          align="center"
          width="300px"
        >
          <template #default="{ row }">
            <el-button
              type="primary"
              size="mini"
              @click="onShowClick(row.id)"
              >{{ $t('msg.btn.show') }}</el-button
            >
            <el-button
              type="warning"
              size="mini"
              @click="onShowClassifyClick(row)"
              v-permission="['editClassify']"
              >{{ $t('msg.btn.edit') }}</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="onRemoveClick(row)"
              v-permission="['removeClassify']"
              >{{ $t('msg.btn.delete') }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <discount-dialog
      v-model="classifyDialogVisible"
      :classifyId="selectClassifyId"
      @saveClassify="getMemberClassifyList"
    ></discount-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { memberClassifyList, deleteMemberClassify } from '@/api/member'
import { watchSwitchLang } from '@/utils/i18n'
import { useRouter } from 'vue-router'
import DiscountDialog from './components/Discount.vue'
import { useI18n } from 'vue-i18n'
import { ElMessage, ElMessageBox } from 'element-plus'
const allMemberClassifies = ref([])
const getMemberClassifyList = async () => {
  allMemberClassifies.value = await memberClassifyList()
}
getMemberClassifyList()
watchSwitchLang(getMemberClassifyList)
const router = useRouter()
// 查看用户详情
const onShowClick = (id) => {
  router.push(`/member/classify/${id}`)
}

const classifyDialogVisible = ref(false)

const selectClassifyId = ref('')

const onShowClassifyClick = (row) => {
  classifyDialogVisible.value = true
  selectClassifyId.value = row.id
}

const classifyAdd = () => {
  classifyDialogVisible.value = true
  selectClassifyId.value = 0
  console.log(selectClassifyId.value)
}
const i18n = useI18n()
const onRemoveClick = (row) => {
  ElMessageBox.confirm(
    i18n.t('msg.classifyInfo.dialogTitle1') +
      row.classifyName +
      i18n.t('msg.classifyInfo.dialogTitle2'),
    {
      confirmButtonText: i18n.t('msg.universal.confirm'),
      type: 'warning',
      cancelButtonText: i18n.t('msg.universal.cancel')
    }
  ).then(async () => {
    await deleteMemberClassify(row.id)
    ElMessage.success(i18n.t('msg.excel.removeSuccess'))
    getMemberClassifyList()
  })
}
</script>

<style lang="scss" scoped>
.member-classify-container {
  .header {
    margin-bottom: 22px;
    text-align: left;
    .float-right {
      float: right;
    }
  }
}
</style>
